<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div>"abba"
        "dog cat cat dog"
    </div>
    <script>

        let pattern = "abba"
        let str = "dog cat cat dog"; // s[i]
        function fn(pattern, str) {

            const obj = {}
            const s = str.split(' ')
            const newarr = [];
            const basic = []
            const newPattern = pattern.split('')

            console.log(s);
            console.log(newPattern);

            if (s.length !== newPattern.length) {
                console.log(1);
                return false
            }
            for (let i = 0; i < newPattern.length; i++) {

                obj[pattern[i]] = obj[pattern[i]] || s[i];
                if (!newarr.includes(s[i]) && !basic.includes(newPattern[i])) {
                    newarr.push(s[i]);  //  dog cat
                    basic.push(newPattern[i]); // a b
                } else {
                    console.log(1);
                    return false
                }
            }
            console.log(obj);


            for (let i = 0; i < newPattern.length; i++) {
                console.log(obj[newPattern[i]], s[i]);
                if (obj[newPattern[i]] !== s[i]) {
                    console.log(1);
                    return false
                }


            }
            return true

        }


        console.log(fn(pattern, str));
        //

        // let pattern = "abba"
        // let str = "dog cat cat dog"; 
        function wordPattern1(pattern, s) {
            const p = pattern.split('') // a b b a  
            const str = s.split(' ') // dog cat cat dog 
            if (p.length !== str.length) {
                return false
            }
            for (let i = 0; i < p.length; i++) {
                if ( p.indexOf(p[i]) !== str.indexOf(str[i]) ) {
                    //  a   >>> 0    b   >>> 1  b   >>>  1  a    >>> 0
                    //  dog >>>> 0   cat >>> 1  cat >>>  1  fish >>>> 3
                    return false
                }
            }
            return true 
        }
    </script>
    <!-- <script>
        //    let pattern = "aaaa"
        //    let str = "dog cat cat dog";
        var wordPattern = function(pattern, s) {
            const news = s.split(' ');
            const newarr = [];
            const basic = {};
            if (pattern.length !== news.length) {
                return false;
            };
            for (let i = 0; i < news.length; i++) {
                basic[pattern[i]] = basic[pattern[i]] || news[i];
                // 判断单词不等于a , b 的情况
                if (basic[pattern[i]] !== news[i]) {
                    return false;
                }
                // 判断，a=b的情况下返回false
                const lastI = news.lastIndexOf(news[i]);
                if( i !== lastI  &&  pattern[i] !== pattern[lastI]){
                    return false;
                }
            }
    };
       
    </script> -->
</body>

</html>